package com.cw1.factory.absFactory.pizzastore.order;

import com.cw1.factory.absFactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author qiuMi
 * @description 披萨 - 订单
 * @Date 2022-10-06 21:57
 **/
public class OrderPizza {
    private AbsFactory absFactory;

    /**
     * 利用构造器直接实现逻辑
     *
     * @param factory 披萨工厂
     */
    public OrderPizza(AbsFactory factory) {
        this.absFactory = factory;
        Pizza pizza = null;
        do {
            String orderType = getType();
            // factory 看是那个子类 -- 北京工厂 / 伦敦工厂
            pizza = absFactory.createPizza(orderType);
            if (orderType == null || pizza == null) {
                System.out.println("订购披萨失败");
                continue;
            }
            // 写出披萨订购过程
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        } while (true);
    }

    /**
     * 获取用户输入的披萨类型
     *
     * @return 披萨类型
     */
    private String getType() {

        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入披萨类型：");
            String type = bufferedReader.readLine();
            return type;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
